/***************************************************************************
 *   Copyright (C) 2014 by Philipp Müller                                  *
 *   philipp.mueller@gmx.de                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
 ***************************************************************************/


import QtQuick 2.0

Rectangle {
    property int key: 0
    property string icon: ''
    property bool delay: true
    property alias text: textItem.text
    property alias color: textItem.color

    signal clicked

    width: 60 * screenScale
    height: 60 * screenScale
    radius: 10 * screenScale

    color: "black"
    border.color : "#a8a8a8"
    border.width : 3 * screenScale

    // color the button with a gradient
    gradient: Gradient {
        GradientStop {
            position: 0.0
            color: {
                if ( mouse.pressed )
                    return "white"
                else
                    return "black"
            }
        }
        GradientStop { position: 1.0; color: "black" }
    }

    Text {
        id: textItem
        anchors.centerIn: parent

        font.pixelSize: 48 * screenScale
        color: "white"
        antialiasing: true
    }

    MouseArea {
        id: mouse
        anchors.fill: parent

        anchors.margins: -5 * screenScale
        onClicked: {
            if( delay ){
              var start = new Date().getTime();
              for (var i = 0; i < 1e7; i++) {
                if ((new Date().getTime() - start) > 100){
                  break;
                }
              }
            }
            console.log( "ButtonKey: key pressed: " + key )
            VompRemote.sendKey( key )
        }
    }

    Image {
        anchors.centerIn: parent
        width: 34 * screenScale
        height: 34 * screenScale
        source: icon
    }
}
